define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('B0250Service', 
        //20210526 modify by kaiinagami for M#93127 補$timeout變數宣告
       ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$timeout',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $timeout) {
            this.init = function (options, feedback) {
                var need_work_reason = false;
                if(options.mode_type == "U")
                    need_work_reason = (config.cache.sys_parameter.EmployeeWorkReason=='1'||config.cache.sys_parameter.EmployeeWorkReason=='3');
                else
                    need_work_reason = (config.cache.sys_parameter.EmployeeWorkReason=='2'||config.cache.sys_parameter.EmployeeWorkReason=='3');
                
                var openDialog =function (data) {
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0250/B0250.html',
                        controller
                    );
                }              
				function controller(dialog) {
                    return {
                        css_url:'module/moduleB/program/B02/B0250/B0250.css',
                        lotInfoList: [] ,
                        mode_type:options.mode_type,
                        parent:options.parent,
                        tmpEmp_no:"",
                        tmpOnWorkInfo:[],
                        panel_show:false,
                        machine_no:options.machineList[0].machine_no,
                        machine_name:options.machineList[0].machine_name,
                        need_work_reason:need_work_reason,
                        reason_no:"",
                        resason_name:"",
                        has_bind_lot:[],
                        user_list:[],
                        user_select_all:false,
                        lot_select_all:false,
                        user_filterSH:{},
                        user_filter:{},
                        lot_filterSH:{},
                        lot_filter:{},

                        //20210311 Modify by德義家興for #0088189: [標準][生產任務中心][DEV]人員上、下工綁定生產批功能優化
                        user_group_title: [],
                        user_group_data: {},
                        user_group_content: [],
                        user_content: [],
                        lot_group_title: [],
                        lot_group_data: {},
                        lot_group_content: [],
                        lot_content: [],

                        init:function()
                        {
                            //初始化 並將今日生產任務 生產批資訊塞入
                            dialog.has_bind_lot = [];
                            dialog.user_list = [];
                            dialog.tmpEmp_no = "";
                            dialog.tmpOnWorkInfo = [];
                            dialog.reason_no = "";
                            dialog.resason_name = "";
                            dialog.has_bind_lot = [];
                            dialog.user_list = [];
                            dialog.user_select_all = false;
                            dialog.lot_select_all = false;
                            dialog.panel_show = false;
                            dialog.user_filter = {};
                            dialog.user_filterSH = {};
                            dialog.lot_filterSH = {};
                            dialog.lot_filter = {};
                            if(dialog.parent == "B0214")
                            {
                                dialog.tmpEmp_no = config.cache.account;
                                dialog.searchEmp();
                            }

                            //20210311 Modify by德義家興for #0088189: [標準][生產任務中心][DEV]人員上、下工綁定生產批功能優化
                            dialog.onWork_user_bind_info_get();
                            dialog.onWork_lot_bind_info_get();
                        },
                        ICCardScanner: function () {
                            $rootScope.OpenScanner({
                                after: function (result) {
                                    $timeout(function () {
                                        dialog.ic_card = result.text;
                                        dialog.searchICCard();
                                    });
                                },
                                exception: function () {
                                    //目前裝置無法使用相機
                                    $rootScope.showAlert($filter('translate')('B0250.showAlert.alert1'));
                                },
                                orientation: 'landscape'
                            });
                        },
                        searchICCard: function (event) {
                            if (!event || event.keyCode == 13) {
                                if (dialog.ic_card != '') {
                                    var content = {
                                        IcNo: dialog.ic_card
                                    }
                                    $MMWService.sendToServer({
                                        uri: 'ServicesSTD.Module_SYS.ic_card_info_get',
                                        content: content,
                                        success: function (data) {
                                            if (data.UserNo != null && data.PassWord != null) {
                                                dialog.tmpEmp_no = data.UserNo;
                                                dialog.searchEmp();//调用员工信息查询方法
                                            }
                                            else {
                                                $rootScope.showAlert($filter('translate')('Login.error.ic_card_error'));
                                                dialog.ic_card = '';
                                                document.getElementById('ic_card').focus();
                                            }
                                        },
                                        error: function (data) {
                                            $rootScope.showAlert(data.Message);
                                            dialog.ic_card = '';
                                            document.getElementById('ic_card').focus();
                                        }
                                    });
                                } else {
                                    $rootScope.showAlert($filter('translate')('B0250.showAlert.alert2'));
                                    dialog.ic_card = '';
                                    document.getElementById('ic_card').focus();
                                }
                            }
                        },
                        searchEmp: function(event)
                        {
                            if(!event || event.keyCode==13)
                            {
                                if(dialog.tmpEmp_no!='')
                                {
                                    dialog.tmpEmp_no = dialog.tmpEmp_no.toUpperCase();
                                    var content = {
                                        machine_no : dialog.machine_no,
                                        employee_no : dialog.tmpEmp_no,
                                        report_datetime : new Date(),
                                        return_type : '10'
                                    };
                                    $MMWService.sendToServer({
                                        uri: 'ServicesSTD.Module_SYS.user_information_get',
                                        content : content,
                                        success : function(data)
                                        {
                                            dialog.tmpOnWorkInfo = data;
                                            dialog.tmpOnWorkInfo.shift_start_time = new Date();                    
                                            var has_machine = $filter("filter")(data.employee_work_detail,{machine_no:dialog.machine_no});
                                            /*if(has_machine  > 0 && dialog.mode_type == "U")
                                            {   
                                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert3'));
                                                if(dialog.parent == "B0214")
                                                    dialog.hide();
                                                return;
                                            }
                                            else if(has_machine <= 0 &&  dialog.mode_type == "D")
                                            {
                                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert4'));
                                                if(dialog.parent == "B0214")
                                                    dialog.hide();
                                                return;
                                            }*/
                                            
                                            var user_list_req = {}
                                            if(dialog.mode_type == "U")
                                                user_list_req.work_mode = "0";
                                            else
                                                user_list_req.work_mode = "1";
                                            user_list_req.user_no = dialog.tmpEmp_no;
                                            user_list_req.equipment_no =  dialog.machine_no;
                                          
                                            load_user_list(user_list_req,function(user_list_data)
                                            {
                                                if(user_list_data.user_list)
                                                {
                                                    dialog.panel_show = user_list_data.user_list.length > 0;
                                                    dialog.user_list = user_list_data.user_list;
                                                } 
                                                if(user_list_data.lot_list)
                                                    dialog.has_bind_lot = user_list_data.lot_list;
                                                if(user_list_data.msg_list && user_list_data.msg_list.length > 0)
                                                    $rootScope.showAlert(user_list_data.msg_list.join("<br>"));
                                                var all_user_num = 0;    
                                                dialog.user_list.forEach(function(item)
                                                {
                                                    item.mark = false;
                                                    if(dialog.mode_type == "U")
                                                    {
                                                        if(item.is_online == false)
                                                        {
                                                            item.mark = true;
                                                            all_user_num++
                                                        }    
                                                    }
                                                    else
                                                    {
                                                        if(item.is_online == true)
                                                        {
                                                            item.mark = true;
                                                            all_user_num++
                                                        }  
                                                    }
                                                });
                                                if(all_user_num > 0 && dialog.user_list.length == all_user_num)
                                                {
                                                    dialog.user_select_all = true;
                                                }
                                                if(dialog.mode_type == "D")
                                                {
                                                    var op_list = [];
                                                    //var lot_list = [];
                                                    var all_lot_num = 0;
                                                    dialog.lotInfoList = $filter('filter')(dialog.has_bind_lot,function(item){
                                                        // 20210406 modifyby 奕達 for M#89495 MSSQL與Oracle傳遞參數大小寫差異
                                                        for(let key in item){
                                                            let UpperCase = key.toUpperCase()
                                                            item[UpperCase] = item[key]
                                                        }
                                                        //20240304 modiffy by wangzy for M#0158736: [sMES] 上工綁定生產批無法綁定到生產批 [應用奈米]
                                                        var lot_op = item.LOT_NO + "_" + item.OP_NO;
                                                        //var is_push = op_list.indexOf(item.OP_NO) == -1 || lot_list.indexOf(item.LOT_NO) == -1;
                                                        var is_push = op_list.indexOf(lot_op) == -1;
                                                        if(is_push)
                                                        {
                                                            op_list.push(lot_op);
                                                            //lot_list.push(item.LOT_NO);
                                                            var has_same = $filter('filter')(dialog.has_bind_lot,{lot_no:item.LOT_NO,op_no:item.OP_NO,user_no:dialog.tmpEmp_no})
                                                            if(has_same.length > 0)
                                                            {
                                                                item.mark = true;
                                                                all_lot_num++;
                                                            }
                                                            else
                                                                item.mark = false;
                                                            item.plot_no = item.LOT_NO;
                                                            item.op_no = item.OP_NO;
                                                            return item;
                                                        } 
                                                    });
                                                    if(all_lot_num > 0 && dialog.lotInfoList.length == all_lot_num)
                                                        dialog.lot_select_all = true;
                                                }
                                                else
                                                {
                                                    var op_list = [];
                                                    //var lot_list = [];
                                                    dialog.lotInfoList = $filter("filter")(options.lotInfoList,function(item)
                                                    {
                                                        item.mark = false;
                                                        if(item.status == "Q" || item.status == "W" || item.status == "R")
                                                        {
                                                            //20240304 modiffy by wangzy for M#0158736: [sMES] 上工綁定生產批無法綁定到生產批 [應用奈米]
                                                            var lot_op = item.plot_no + "_" + item.op_no;
                                                            //var is_push = op_list.indexOf(item.op_no) == -1 || lot_list.indexOf(item.plot_no) == -1;
                                                            var is_push = op_list.indexOf(lot_op) == -1;
                                                            if(is_push)
                                                            {
                                                                op_list.push(lot_op);
                                                                //lot_list.push(item.plot_no);
                                                                return item;
                                                            } 
                                                        }                                    
                                                    });
                                                    if(dialog.lotInfoList)
                                                    {
                                                        var all_lot_num = 0;
                                                        dialog.lotInfoList.forEach(function(item){
                                                        var c = $filter("filter")(dialog.has_bind_lot,{lot_no:item.plot_no,op_no:item.op_no});
                                                        if(c && c.length > 0)
                                                        {
                                                            item.mark = true;
                                                            all_lot_num++
                                                        }
                                                            return item;
                                                        });
                                                        if(all_lot_num > 0 && dialog.lotInfoList.length == all_lot_num)
                                                            dialog.lot_select_all = true;
                                                    }
                                                }
                                            });    
                                        },
                                        error:function(data){
                                            $rootScope.showAlert(data.Message);
                                            document.getElementById('tmpEmp_no').focus();
                                            if(dialog.parent == "B0214")
                                                dialog.hide();
                                        }
                                    });
                                }
                                else
                                {
                                    $rootScope.showAlert($filter('translate')('B0250.showAlert.alert5'));
                                    document.getElementById('tmpEmp_no').focus();
                                }
                            }
                        },
                        loadEmpWorkReason:function()
                        {
                            var reason_type = '0';
                            var title = $filter('translate')('B0250.title_reasion1');
                            if(dialog.mode_type == "D")
                            {
                                reason_type = '1';
                                title = $filter('translate')('B0250.title_reasion2');
                            }
                            $MMWService.pageNumberInfoGet({
                                page_mode : '35',
                                query_condition_info:[
                                    {column_name:'REASONTYPE', query_mode:'0', value:reason_type, merge_condition_model:'AND' },
                                    {column_name:'ISSUESTATE', query_mode:'0', value:'2', merge_condition_model:'AND' }
                                ]
                            }, function(items){
                                if(items && items.length > 0){
                                    $rootScope.showSelect({
                                        label : function(item){
                                            return item.page_name + '(' + item.page_no + ')';
                                        },
                                        title: title,
                                        code : 'page_no',
                                        selectCode : dialog.reason_no || '',
                                        list : items,
                                        confirm : function(selectItem, radio_dialog){
                                            radio_dialog.hide();
                                            dialog.reason_no = selectItem.page_no;
                                            dialog.resason_name = selectItem.page_name;
                                        }
                                    });
                                } else {
                                    $rootScope.showAlert($filter('translate')('B0250.showAlert.alert6'));
                                }
                            });
                        },
                        getState:function(item) 
                        {
                            if(item.is_online)
                                return $filter('translate')('B0250.state1');
                            else
                                return $filter('translate')('B0250.state2');
                        },
                        userItemClick:function(item)
                        {
                            if(item.mark)
                                item.mark = false;
                            else
                                item.mark = true;
                        },
                        userItemSelectAll:function()
                        {
                            if(dialog.user_select_all)
                            {
                                dialog.user_list.forEach(function(item){
                                        item.mark = false;
                                });
                                dialog.user_select_all = false;
                            }
                            else
                            {
                                dialog.user_list.forEach(function(item){
                                        item.mark = true;
                                });
                                dialog.user_select_all = true;
                            }
                        },
                        lotItemSelectAll:function()
                        {
                            if(dialog.lot_select_all)
                            {
                                dialog.lotInfoList.forEach(function(item)
                                {
                                    if(dialog.work_mode == "U")
                                    {
                                        var has_lot = $filter("filter")(dialog.has_bind_lot,{lot_no:item.plot_no});
                                        if(has_lot.length <= 0)
                                            item.mark = false;
                                    }
                                    else
                                    {
                                        item.mark = false;
                                    }
                                });
                                dialog.lot_select_all = false;
                            }
                            else
                            {
                                dialog.lotInfoList.forEach(function(item){
                                    if(dialog.work_mode == "U")
                                    {
                                        var has_lot = $filter("filter")(dialog.has_bind_lot,{lot_no:item.plot_no});
                                        if(has_lot.length <= 0)
                                            item.mark = true;
                                    }
                                    else
                                    {
                                        item.mark = true;
                                    }
                                });
                                dialog.lot_select_all = true;
                            }
                        },
                        lotItemClick:function(item)
                        {
                            if(dialog.work_mode == "U")
                            {
                                var has_lot = $filter("filter")(dialog.has_bind_lot,{lot_no:item.plot_no})
                                if(has_lot.length <= 0)
                                {
                                    if(item.mark)
                                        item.mark = false;
                                    else
                                        item.mark = true;
                                }
                            }
                            else
                            {
                                if(item.mark)
                                    item.mark = false;
                                else
                                    item.mark = true;
                            }
                        },
                        work_up_and_lot_bind_confirm: function (){
                            var user_list = $filter("filter")(dialog.user_list,dialog.user_filter);
                            var lot_list = $filter("filter")(dialog.lotInfoList,dialog.lot_filter);
                            if(user_list.length <= 0)
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert7'));
                                return;
                            }
                            if(dialog.need_work_reason && dialog.reason_no == "")
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert6'));
                                return;
                            }
                            var content = {};
                            var to_date = new Date();
                            content.mode_type  = "0";
                            content.employeewrokset = [];
                            content.bind_lot_list = [];
                            user_list.forEach(function(item)
                            {
                                if(item.mark)
                                {
                                    var emp_obj = {};
                                    emp_obj.machine_no = dialog.machine_no;
                                    emp_obj.machine_name = dialog.machine_name;
                                    emp_obj.employee_no = item.user_no;
                                    emp_obj.work_datetime = to_date;
                                    emp_obj.reason_no = dialog.reason_no;
                                    content.employeewrokset.push(emp_obj);
                                    lot_list.forEach(function(item2)
                                    {
                                        var has_lot = $filter("filter")(dialog.has_bind_lot,{lot_no:item2.plot_no,op_no:item2.op_no,user_no:item.user_no});
                                        if(has_lot.length <= 0 && item2.mark)
                                        {
                                            var lot_obj = {};
                                            lot_obj.plot_no = item2.plot_no;
                                            lot_obj.machine_no = dialog.machine_no;
                                            lot_obj.op_no = item2.op_no;
                                            lot_obj.employee_no = item.user_no;
                                            content.bind_lot_list.push(lot_obj);
                                        }
                                    });
                                }
                            });
                            run_user_process(content,function(){
                                dialog.hide();
                                feedback();
                            });
                        },
                        work_down_and_lot_release_confirm:function(){
                            var user_list = $filter("filter")(dialog.user_list,dialog.user_filter);
                            var lot_list = $filter("filter")(dialog.lotInfoList,dialog.lot_filter);
                            if(user_list.length <= 0)
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert7'));
                                return;
                            }
                            if(dialog.need_work_reason && dialog.reason_no == "")
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert6'));
                                return;
                            }
                            var content = {};
                            var to_date = new Date();
                            content.mode_type  = "3";
                            content.employeewrokset = [];
                            content.bind_lot_list = [];
                            user_list.forEach(function(item)
                            {
                                if(item.mark)
                                {
                                    var emp_obj = {};
                                    emp_obj.machine_no = dialog.machine_no;
                                    emp_obj.machine_name = dialog.machine_name;
                                    emp_obj.employee_no = item.user_no;
                                    emp_obj.work_datetime = to_date;
                                    emp_obj.reason_no = dialog.reason_no;
                                    content.employeewrokset.push(emp_obj);
                                    lot_list.forEach(function(item2)
                                    {
                                        if(item2.mark)
                                        {
                                            var lot_obj = {};
                                            lot_obj.plot_no = item2.plot_no;
                                            lot_obj.machine_no = dialog.machine_no;
                                            lot_obj.op_no = item2.op_no;
                                            lot_obj.employee_no = item.user_no;
                                            content.bind_lot_list.push(lot_obj);
                                        }
                                    });
                                }
                            });
                            run_user_process(content,function(){
                                dialog.hide();
                                feedback();
                            });
                        },
                        work_down_confirm:function(){
                            var user_list = $filter("filter")(dialog.user_list,dialog.user_filter);
                            if(user_list.length <= 0)
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert7'));
                                return;
                            }
                            if(dialog.need_work_reason && dialog.reason_no == "")
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert6'));
                                return;
                            }
                           
                            var content = {};
                            var to_date = new Date();
                            content.mode_type  = "1";
                            content.employeewrokset = [];
                            content.bind_lot_list = [];
                            user_list.forEach(function(item)
                            {
                                if(item.mark)
                                {
                                    var emp_obj = {};
                                    emp_obj.machine_no = dialog.machine_no;
                                    emp_obj.machine_name = dialog.machine_name;
                                    emp_obj.employee_no = item.user_no;
                                    emp_obj.work_datetime = to_date;
                                    emp_obj.reason_no = dialog.reason_no;
                                    content.employeewrokset.push(emp_obj);
                                }
                            });
                            run_user_process(content,function(){
                                dialog.hide();
                                feedback();
                            });
                        },
                        lot_release_confirm:function(){
                            var user_list = $filter("filter")(dialog.user_list,dialog.user_filter);
                            var lot_list = $filter("filter")(dialog.lotInfoList,dialog.lot_filter);
                            if(user_list.length <= 0)
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert7'));
                                return;
                            }
                            if(lot_list.length <= 0)
                            {
                                $rootScope.showAlert($filter('translate')('B0250.showAlert.alert8'));
                                return;
                            }
                            var content = {};
                            content.mode_type  = "2";
                            content.employeewrokset = [];
                            content.bind_lot_list = [];
                            user_list.forEach(function(item)
                            {
                                if(item.mark)
                                {
                                    lot_list.forEach(function(item2)
                                    {
                                        if(item2.mark)
                                        {
                                            var lot_obj = {};
                                            lot_obj.plot_no = item2.plot_no;
                                            lot_obj.machine_no = dialog.machine_no;
                                            lot_obj.op_no = item2.op_no;
                                            lot_obj.employee_no = item.user_no;
                                            content.bind_lot_list.push(lot_obj);
                                        }
                                    });
                                }
                            });
                            run_user_process(content,function(){
                                dialog.hide();
                                feedback();
                            });
                        },

                        //20210311 Modify by德義家興for #0088189: [標準][生產任務中心][DEV]人員上、下工綁定生產批功能優化
                        onWork_user_bind_info_get: function () { //人員資料查詢
                            var title = []; //暫存title
                            $MMWService.pageNumberInfoGet({
                                page_mode: 'onWork_user_bind_info_get',
                                query_condition_info: [
                                    { column_name: 'ts.loginplaceno', query_mode: '0', value: dialog.machine_no, merge_condition_model: 'AND' }
                                ]
                            }, function (data) {
                                //抓出群組的title
                                data.forEach(function (item) {
                                    title.push(item.v_user);
                                })
                                //去除重複
                                dialog.user_group_title = title.filter(function (element, index, arr) {
                                    return arr.indexOf(element) === index;
                                });
                                //設定好群組的數量
                                for (let index = 0; index < dialog.user_group_title.length; index++) {
                                    dialog.user_group_content.push([]);
                                };
                                //原始資料
                                dialog.user_group_data = data;

                                var count = 0;
                                dialog.user_content = dialog.user_group_content;
                                //把未分類的原始資料 依照title分組
                                dialog.user_group_title.forEach(function (title) {
                                    dialog.user_content[count] = [];
                                    dialog.user_group_data.forEach(function (content) {
                                        if (title === content.v_user) {
                                            dialog.user_content[count].push(content); //放列物件
                                            dialog.user_content[count].is_close = false; //顯示的開關
                                        }
                                    })
                                    count++;
                                })
                            });
                        },
                        onWork_lot_bind_info_get: function () { //生產批資料查詢
                            var title = []; //暫存title
                            $MMWService.pageNumberInfoGet({
                                page_mode: 'onWork_lot_bind_info_get',
                                query_condition_info: [
                                    { column_name: 'recs.eqpno', query_mode: '0', value: dialog.machine_no, merge_condition_model: 'AND' }
                                ]
                            }, function (data) {
                                //抓出群組的title
                                data.forEach(function (item) {
                                    title.push(item.v_lot);
                                })
                                //去除重複
                                dialog.lot_group_title = title.filter(function (element, index, arr) {
                                    return arr.indexOf(element) === index;
                                });
                                //設定好群組的數量
                                for (let index = 0; index < dialog.lot_group_title.length; index++) {
                                    dialog.lot_group_content.push([]);
                                };
                                //原始資料
                                dialog.lot_group_data = data;

                                var count = 0;
                                dialog.lot_content = dialog.lot_group_content;
                                //把未分類的原始資料 依照title分組
                                dialog.lot_group_title.forEach(function (title) {
                                    dialog.lot_content[count] = [];
                                    dialog.lot_group_data.forEach(function (content) {
                                        if (title === content.v_lot) {
                                            dialog.lot_content[count].push(content); //放列物件
                                            dialog.lot_content[count].is_close = false; //顯示的開關
                                        }
                                    })
                                    count++;
                                })
                            });
                        },
                        openDialogUserBindInfo: function () { //人員查詢btn
                            $mdDialog.dialog(
                                'module/moduleB/program/B02/B0250/UserBindInfo.tmp.html',
                                function (user) {
                                    return {
                                        group_title: dialog.user_group_title, //群組的title
                                        group_data: dialog.user_group_data, //未分類的原始資料
                                        rawContent: dialog.user_content, //分組好的資料
                                        content: dialog.user_content, //篩選後的資料;與html連結
                                        userNo: "",
                                        // 查詢按鈕
                                        btn: function () {
                                            if (!user.userNo.length == 0) { //判斷input是否有輸入
                                                user.content = [];
                                                user.rawContent.forEach(function (group) {
                                                    if ((group[0].v_user).indexOf(user.userNo) != -1) {
                                                        user.content.push(group);
                                                    };
                                                })
                                            } else user.content = dialog.user_content; //還原未篩選的資料
                                        },
                                        // 按enter
                                        enter: function (event) {
                                            if (event.keyCode == 13) {
                                                user.btn();
                                            }
                                        },
                                        // 收放鈕功能
                                        triggerGroup: function (group) {
                                            if (group.is_close == true) {
                                                group.is_close = false;
                                            } else {
                                                group.is_close = true;
                                            }
                                        },
                                        back: function () {
                                            user.hide();
                                        },
                                        confirm: function () {
                                            user.hide();
                                        },
                                    }
                                }
                            )
                        },
                        openDialogLotBindInfo: function () { //生產批查詢btn
                            $mdDialog.dialog(
                                'module/moduleB/program/B02/B0250/LotBindInfo.tmp.html',
                                function (lot) {
                                    return {
                                        group_title: dialog.lot_group_title, //群組的title
                                        group_data: dialog.lot_group_data, //未分類的原始資料
                                        rawContent: dialog.lot_content, //分組好的資料
                                        content: dialog.lot_content, //篩選後的資料;與html連結
                                        lotNo: "",
                                        // 查詢按鈕
                                        btn: function () {
                                            if (!lot.lotNo.length == 0) { //判斷input是否有輸入
                                                lot.content = [];
                                                lot.rawContent.forEach(function (group) {
                                                    if ((group[0].v_lot).indexOf(lot.lotNo) != -1) {
                                                        lot.content.push(group);
                                                    };
                                                })
                                            } else lot.content = dialog.lot_content; //還原未篩選的資料
                                        },
                                        // 按enter
                                        enter: function (event) {
                                            if (event.keyCode == 13) {
                                                lot.btn();
                                            }
                                        },
                                        // 收放鈕功能
                                        triggerGroup: function (group) {
                                            if (group.is_close == true) {
                                                group.is_close = false;
                                            } else {
                                                group.is_close = true;
                                            }
                                        },
                                        back: function () {
                                            lot.hide();
                                        },
                                        confirm: function () {
                                            lot.hide();
                                        },
                                    }
                                }
                            )
                        },

                        back: function () {
                            dialog.hide();
                            feedback();
                        }
                    }
                }
              

				openDialog();
            };
            this.employee_wrok_set_json = function(options,feedback)
            {
                var need_work_reason = (config.cache.sys_parameter.EmployeeWorkReason=='2'||config.cache.sys_parameter.EmployeeWorkReason=='3');
            
                $rootScope.showConfirm($filter('translate')('B0250.confirmAlert.alert1'),function(dialog)
                {
                    dialog.hide();
                    var content = {};
                    var now_datetime = moment().format('YYYY/MM/DD HH:mm:ss');
                    content.mode_type = "4";
                    content.employeewrokset = [];
                    if(need_work_reason)
                    {
                        loadEmpWorkReason('',function(selectItem)
                        {
                            options.machine_list.forEach(function(machine)
                            {
                                var emp_obj = {};
                                emp_obj.reason_no = selectItem.page_no;
                                emp_obj.machine_no = machine.machine_no;
                                emp_obj.employee_no = config.cache.account;
                                emp_obj.work_datetime = now_datetime;
                                content.employeewrokset.push(emp_obj);  
                            });
                            run_user_process(content,function(){
                                feedback();
                            });
                        });
                    }
                    else
                    {
                        options.machine_list.forEach(function(machine)
                        {
                            var emp_obj = {};
                            emp_obj.reason_no = '';
                            emp_obj.machine_no = machine.machine_no;
                            emp_obj.employee_no = config.cache.account;
                            emp_obj.work_datetime = now_datetime;
                            content.employeewrokset.push(emp_obj);  
                        });
                        run_user_process(content,function(){
                            feedback();
                        });
                    }
                });
            };
            //人員清單
            function load_user_list(content,callback)
            {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_SYS.user_work_bind_lot_info_get',
                    content : content,
                    success : function(data)
                    {
                        callback(data);           
                    },
                    error:function(data){
                        
                    }
                });
            }
            //執行生產批綁定機制
            function run_user_process(content,callback)
            {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_SYS.user_work_bind_lot_process',
                    content : content,
                    success : function(data)
                    {
                        if(data.msg_list && data.msg_list.length > 0)
                            $rootScope.showAlert(data.msg_list.join("<br>"));
                        else
                            $rootScope.showToast($filter('translate')('B0250.showAlert.success'));  
                        callback();        
                    },
                    error:function(data)
                    {
                        $rootScope.showAlert(data.Message);
                    }
                });
            }
            // 人員原因基本資料取得
            function loadEmpWorkReason(reason_no, callback)
            {
                $MMWService.pageNumberInfoGet({
                    page_mode : '35',
                    query_condition_info:[
                        {column_name:'REASONTYPE', query_mode:'0', value:'1', merge_condition_model:'AND' },
                        {column_name:'ISSUESTATE', query_mode:'0', value:'2', merge_condition_model:'AND' }
                    ]
                }, function(items){
                    if(items && items.length > 0){
                        $rootScope.showSelect({
                            label : function(item){
                                return item.page_name + '(' + item.page_no + ')';
                            },
                            title: $filter('translate')('B0250.title_reasion2'),
                            code : 'page_no',
                            selectCode : reason_no || '',
                            list : items,
                            confirm : function(selectItem, radio_dialog){
                                radio_dialog.hide();
                                if (callback)
                                    callback(selectItem);
                            }
                        });
                    } else {
                        $rootScope.showAlert($filter('translate')('B0250.showAlert.alert6'));
                    }
                });
            }
        }]);
});

